﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSWXBLPDA
{
    public partial class RESCANF : Form
    {
       private LoginReference.Registration Reg = new BSWXBLPDA.LoginReference.Registration();
        private string name ;
        private string ID;
        private string FID;


        public RESCANF()
        {
            InitializeComponent();
        }

        public RESCANF(string _ID, string _NAME)
        {
          InitializeComponent();
            this.ID = _ID;
            this.name = _NAME;
        }

        private void RESCANF_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BindData();
        }



        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "FCARD";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "ROWNUM";
            cl1.Width = 30;
            dts.GridColumnStyles.Add(cl1);


            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "入库单号";
            cl2.MappingName = "FID";
            cl2.Width = 220;
            dts.GridColumnStyles.Add(cl2);


            dataGrid1.TableStyles.Add(dts);

        }



        private void BindData()
        {
            DataSet ds = Reg.GetFCARDList();
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
            dataGrid1.DataSource = dt;
            //}
        }   

        ////获取F卡码
        private void tb_FCARD_TextChanged(object sender, EventArgs e)
        {
            string FCARD = tb_FCARD.Text.Trim();
            bool result = false;
            if (FCARD.Length == 26)
            {
                result = Reg.GetFCARD(FCARD);
                if (result == true)
                {
                    tb_UPDATER.Text = this.name;
                    tb_UPDATETIME.Text = DateTime.Now.ToString();

                    button1.Focus();
                    DataGridStyle();
                    BindData();
                }
                else {
                    MessageBox.Show("该条码在数据库中不存在，请扫描F卡", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    tb_FCARD.Text = "";
                }
               
            }
        }

        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            Main iw = new Main(this.ID, this.name);
            iw.Show();
            this.Hide();
        }

        // 登记NG
        private void button1_Click(object sender, EventArgs e)
        {
            string FCARD = tb_FCARD.Text.Trim();

            bool result = false;
            if (FCARD == null || FCARD == "")
            {
                MessageBox.Show("请扫描F卡", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            }
           string UPDATETIME = tb_UPDATETIME.Text;
            result = Reg.UpdateStateNG(FCARD, this.name, UPDATETIME);
         
            if (result == true)
            {
                tb_FCARD.Text = "";
                tb_UPDATER.Text = "";
                tb_UPDATETIME.Text = "";
                tb_FCARD.Focus();

                DataGridStyle();
                BindData();
            }
            else
            {
                MessageBox.Show("更新NG失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tb_FCARD.Text = "";
                tb_UPDATER.Text = "";
                tb_UPDATETIME.Text = "";
                tb_FCARD.Focus();
            }
        }

        // 登记OK
        private void button3_Click(object sender, EventArgs e)
        {
            string FCARD = tb_FCARD.Text.Trim();
            bool result = false;
            if (FCARD == null || FCARD == "")
            {
                MessageBox.Show("请扫描F卡", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

            }
            string UPDATETIME = tb_UPDATETIME.Text;
            result = Reg.UpdateStateOK(FCARD, this.name, UPDATETIME);
            if (result == true)
            {
                tb_FCARD.Text = "";
                tb_UPDATER.Text = "";
                tb_UPDATETIME.Text = "";
                tb_FCARD.Focus();

                DataGridStyle();
                BindData();
            }
            else
            {
                MessageBox.Show("更新OK失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tb_FCARD.Text = "";
                tb_UPDATER.Text = "";
                tb_UPDATETIME.Text = "";
                tb_FCARD.Focus();
            }
        }

        //返回
        private void button2_Click_1(object sender, EventArgs e)
        {
            Main iw = new Main(this.ID, this.name);
            iw.Show();
            this.Hide();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FID = ((DataTable)(dataGrid1.DataSource)).Rows[index]["FID"].ToString();

            tb_FCARD.Text = FID;
            tb_UPDATER.Text = this.name;
            tb_UPDATETIME.Text = DateTime.Now.ToString();
            button1.Focus();
        }

     
    }
}